<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
*/

/**
 * Description of model_transaksi
 *
 * @author Hadi
 */
class model_kas_operasional extends Default_My_Model {

    function __construct() {
        parent::__construct();
    }

    function getQueryKasOperasional($whr, $offset = 0, $length = 30, $only_row_count = false, $export_mode = false) {
        if ($export_mode){            
            $field_list = "k.tanggal_ko, jko.nama_jko, sjko.nama_sjko, k.keterangan_ko, k.bukti_kas, @km_ko := k.kas_masuk_ko as kas_masuk, @kk_ko := k.kas_keluar_ko as kas_keluar, @saldo_ko := @saldo_ko + @km_ko - @kk_ko AS saldo";            
        } else if ($only_row_count) {
            $field_list = "count(k.id_ko) as jumlah";
        } else {
            $field_list = "k.*, mk.nama as nama_karyawan, jko.nama_jko, sjko.nama_sjko, @km_ko := k.kas_masuk_ko as kas_masuk, @kk_ko := k.kas_keluar_ko as kas_keluar, @saldo_ko := @saldo_ko + @km_ko - @kk_ko AS saldo";
        }               
        if ($length > 0){
            $batas = "LIMIT $length OFFSET $offset";
        }
        $sql = "SELECT ".$field_list."
                FROM kas_operasional k
                LEFT JOIN mt_karyawan mk ON k.submitter_ko = mk.nik
                LEFT JOIN sub_jenis_kas_operasional sjko on sjko.id_sjko = k.id_sjko
                LEFT JOIN jenis_kas_operasional jko on jko.id_jko = sjko.id_jko
                $whr ORDER by k.tanggal_ko asc, k.id_ko asc ".$batas;                
        return $sql;
    }
    
    function initSaldo($whr, $offset = 0, $length= 30, $start_date) {
        $sql = $this->getQueryKasOperasional($whr, $offset, $length);
        $result = $this->db->query($sql);
        $total = 0;
        foreach ($result->result() as $row) {            
            $total = $this->get_scalar("select sum(kas_masuk_ko) - sum(kas_keluar_ko) as total from kas_operasional where id_ko < $row->id_ko","total");        
            break;
        } 
        if ($total == null){
            $total = 0;            
        }
        $this->db->query("select @saldo_ko:=".$total." as x");
    }

    function getKasOperasionalData($whr, $offset = 0, $length= 30) {        
        $sql = $this->getQueryKasOperasional($whr, $offset, $length);
        return $this->db->query($sql);
    }

    function getKasOperasionalDataRC($whr) {
        $sql = $this->getQueryKasOperasional($whr, 0, 1, true);
        $resultSet = $this->db->query($sql);
        $result = $resultSet->row();
        return $result->jumlah;
    }

    function getSaldoKasOperasional() {
        return $this->get_scalar("select sum(kas_masuk_ko) - sum(kas_keluar_ko) as total from kas_operasional","total");        
    }
    
    function getSaldoKasOperasionalSaatIni($last_periode) {
        $total  = $this->get_scalar("select sum(kas_masuk_ko) - sum(kas_keluar_ko) as total from kas_operasional where tanggal_ko < '" . date("Y-m", strtotime($last_periode)) . "-01' ","total");                        
        return $total;        
    }

    function insertKasOperasional($data) {
        $set = array(
                "tanggal_ko" => $data["tanggal_ko"],
                "kas_masuk_ko" => $data["kas_masuk_ko"],
                "bukti_kas" => $data["bukti_kas"],
                "kas_keluar_ko" => $data["kas_keluar_ko"],
                "keterangan_ko" => $data["keterangan_ko"],
                "submitter_ko" => $data["submitter_ko"],
                "date_created_ko" => $data["date_created_ko"],
                "id_sjko" => $data["id_sjko"],
                "reff_id" => $data["reff_id"],
                "reff_tabel" => $data["reff_tabel"]
        );
        $this->db->insert("kas_operasional", $set);
        return $this->db->insert_id();
    }

    function updateKasOperasional($data, $id) {
        $set = array(
                "tanggal_ko" => $data["tanggal_ko"],
                "kas_masuk_ko" => $data["kas_masuk_ko"],
                "bukti_kas" => $data["bukti_kas"],
                "kas_keluar_ko" => $data["kas_keluar_ko"],
                "keterangan_ko" => $data["keterangan_ko"],
                "submitter_ko" => $data["submitter_ko"],
                "date_created_ko" => $data["date_created_ko"],
                "id_sjko" => $data["id_sjko"]
        );
        $this->db->update("kas_operasional", $set, "id_ko = ".$id);
    }

    function removeDataKasOperasional($id) {
        $this->db->where('id_ko', $id);
        $this->db->delete('kas_operasional', $where);
    }

}

?>
